package es.cloudcast.server.persistence.jdo.action.basic;

import java.io.Serializable;

import javax.jdo.PersistenceManager;

import es.cloudcast.server.persistence.PersistenceException;
import es.cloudcast.server.persistence.dao.jdo.PersistenceManagerFactory;
import es.cloudcast.server.persistence.jdo.action.Action;

/**
 * Checks if an instance exists or not
 * 
 * @author Samuel García Martínez <in0de>
 *
 */
public class ExistsAction implements Action<Boolean> {

	protected Serializable id;
	protected Class<?> clazz;
	
	public ExistsAction(Serializable id, Class<?> clazz) {
		this.id = id;
		this.clazz = clazz;
	}
	
	public Boolean process(PersistenceManagerFactory PMF)
			throws PersistenceException {
		Boolean result = false;
		
		try {
			PersistenceManager pManager = 
				PMF.getCurrentPersistenceManager();
			Object instance = pManager.getObjectById(clazz, id);
			
			result = instance != null;
			
			pManager.evict(instance);
		} catch(Exception ex) {
			throw new PersistenceException(ex);
		}
		
		return result;
	}

}
